const stationService = require('../service/station');
const retCode = require('../utils/retcode');


class StationController {

  /**
   * 分页查询
   * @param  {object} ctx   上下文
   * @return {object}       结果
   */

  async StationDataFilter(ctx) {
    let form = ctx.request.body;
    // console.log(form)
    const args = {
      current: parseInt(form.current),
      pageSize: parseInt(form.pageSize),
      staName: form.staName,
      ouName: form.ouName
    };
    // console.log(args)


    let ListResult = await stationService.getListByFilter(args);


    // let NumResult = await stationService.getCountByFilter(args);

    let ResultData = {
      ListResult: ListResult.rows,
      current: args.current,
      // total: NumResult[0]['count(1)'],
      total: ListResult.count,
      pageSize: args.pageSize
    }
    ctx.body = {
      status: 200,
      statusText: 'ok',
      data: ResultData
    };
    // console.log(ResultData.pageSize)
    // console.log(ResultData.ListResult)
    // console.log(ResultData.total)


    // This is needed in order to expose the length in `Content-Range` header.
    // ctx.pagination.length = userNumResult[0].total

  }



  async delStaData(ctx) {
    let form = ctx.request.body;
    // console.log(form)
    if (form.values.STA_ID == null){ //如果没有传STA_ID 直接返回
      ctx.body = {
        status: 400,
        statusText: 'fail',
        data: {}
      };
      return
    }


    const args = {
      value: form.values,
    };

    let Result = await stationService.delStaData(args)

    if (Result.affectedRows >= 0) { // 重复删除  算成功
      ctx.body = {
        status: 200,
        statusText: 'ok',
        // data: ResultData
      };
    } else {
      ctx.body = {
        status: 400,
        statusText: 'fail',
        data: {}
      };
    }

  }
  //
  async updateStaData(ctx) {
    let form = ctx.request.body;
    // console.log(form)
    if (form.values.STA_ID == null){ //如果没有传STA_ID 直接返回
      ctx.body = {
        status: 400,
        statusText: 'fail',
        data: {}
      };
      return
    }
    const args = {
      value: form.values,
    };


    let Result = await stationService.updateStaData(args)

    if (Result.affectedRows != 0) {
      ctx.body = {
        status: 200,
        statusText: 'ok',
        // data: ResultData
      };

    } else {
      ctx.body = {
        status: 400,
        statusText: 'fail',
        data: {}
      };
    }

  }

  async getStaName(ctx) {
    let form = ctx.request.body;
    console.log(form)
    const args = {
      OUName: form.ouName,
    };
    let Result = await stationService.getStaName(args);
    // console.log(Result)
    let arr = [];
    try {
      Result.forEach(function (e, i) {
        arr.push(e.STA_NAME)
      });
      // console.log("sta_name length",arr)
      // console.log(arr)
      ctx.body = {
        status: 200,
        statusText: 'ok',
        data: arr
      };
    } catch (e) {
      ctx.body = {
        status: 400,
        statusText: 'fail',
        data: arr
      };
    }

  }

  async getOuName(ctx) {
    let args = {}
    let Result = await stationService.getOuName(args);
    // console.log(Result)
    let arr = [];
    try {
      Result.forEach(function (e, i) {
        arr.push(e.OUName)
      });
      // console.log(arr)
      ctx.body = {
        status: 200,
        statusText: 'ok',
        data: arr
      };
    } catch (e) {
      ctx.body = {
        status: 400,
        statusText: 'fail',
        data: arr
      };
    }

  }

  async creatStaData(ctx) {
    let form = ctx.request.body;
    // console.log(form);
    const args = {
      value: form.values,
    };


    let Result = await stationService.creatStaData(args)

    if (Result.affectedRows != 0) {
      ctx.body = {
        status: 200,
        statusText: 'ok',
        // data: ResultData
        data:{}
      };

    } else {
      ctx.body = {
        status: 400,
        statusText: 'fail',
        data: {}
      };
    }

  }

}


module.exports = new StationController();
